package com.easepal802s.project.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.R;
import com.easepal802s.project.ble.BleController;
import com.easepal802s.project.ui.iview.IFraAirView;
import com.easepal802s.project.ui.presenter.FraAirPresenter;
import com.easepal802s.project.view.LongClickImageView;
import com.easepal802s.project.widget.LayoutBody;
import com.easepal802s.project.widget.LayoutFunctionView;
import com.easepal802s.project.widget.LayoutGears5;
import com.easepal802s.project.view.MoveLayout;
import com.ogawa.base.utils.CommonUtil;
import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Fhb on 2018/3/29.
 */
public class AirPressureMassageFragment extends Fragment implements View.OnClickListener,IFraAirView,MoveLayout.OnChooseAction {

    private View inflate;

    private LinearLayout mLlAirPosition;

    private LayoutBody mLayoutBody;
    private LayoutFunctionView mLayoutFunctionView;
    private FraAirPresenter mFraAirPresenter;

    private TextView tvTime;
    private LayoutGears5 mLlAirStrong;
    private LongClickImageView mIvAddStrong;  //强度
    private LongClickImageView mIvSubStrong; //强度
    private ImageView mIvShoulder;
    private ImageView mIvBack;
    private ImageView mIvHand;
    private ImageView mIvLeg;
    private ImageView mIvFoot;
    private List<ImageView> mIvAirPosition = new ArrayList<>();
    private MyHandler myHandler;
    private String[] name = {"充气位置","强度"};
    private MoveLayout mLayout;
    private ScrollView mScroll;

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_air_pressure_massage, container, false);
        initView();
        return inflate;
    }

    private void initView() {
        mFraAirPresenter = new FraAirPresenter(this);
        myHandler = new MyHandler(this);
        mIvShoulder = inflate.findViewById(R.id.fragment_air_shoulder);
        mIvBack = inflate.findViewById(R.id.fragment_mt_iv_back);
        mIvHand = inflate.findViewById(R.id.fragment_mt_iv_hand);
        mIvLeg = inflate.findViewById(R.id.fragment_mt_iv_leg);
        mIvFoot = inflate.findViewById(R.id.fragment_mt_iv_foot);
        mScroll =  inflate.findViewById(R.id.fragment_air_scoll);
        mLayout=  inflate.findViewById(R.id.air_fragment_move_layout);
        mLayout.setData(name);
        mLayout.setScollView(mScroll);
        mLayout.setAction(this);

        mIvAirPosition.clear();
        mIvAirPosition.add(mIvShoulder);
        mIvAirPosition.add(mIvBack);
        mIvAirPosition.add(mIvHand);
        mIvAirPosition.add(mIvLeg);
        mIvAirPosition.add(mIvFoot);


        mLlAirPosition = inflate.findViewById(R.id.linear_methods_to_select);
        mLlAirStrong = inflate.findViewById(R.id.rl_air_strong);
        mIvAddStrong = inflate.findViewById(R.id.iv_gears5_add);
        mIvSubStrong = inflate.findViewById(R.id.iv_gears5_sub);
        tvTime = inflate.findViewById(R.id.showTime);
        mLayoutBody= inflate.findViewById(R.id.layout_body_view);
        mLayoutFunctionView = inflate.findViewById(R.id.fragment_air_layout_function);

        tvTime.setOnClickListener(this);
        mIvAddStrong.setOnClickListener(this);
        mIvSubStrong.setOnClickListener(this);

        mIvShoulder.setOnClickListener(this);
        mIvBack.setOnClickListener(this);
        mIvHand.setOnClickListener(this);
        mIvLeg.setOnClickListener(this);
        mIvFoot.setOnClickListener(this);

    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLayoutBody.onDestory();
        mLayoutFunctionView.onDestory();
        mFraAirPresenter.OnDestory();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.showTime:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.TIME, true);
                }
                break;
            case R.id.iv_gears5_add:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.CONGQIQDADD, true);
                }
                break;
            case R.id.iv_gears5_sub:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.CONGQIQDSUB, true);
                }
                break;
            case R.id.fragment_air_shoulder:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.JIANB, true);
                }
                break;
            case R.id.fragment_mt_iv_back:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.BEIB, true);
                }
                break;
            case R.id.fragment_mt_iv_hand:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.SHOUB, true);
                }
                break;
            case R.id.fragment_mt_iv_leg:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.TUIB, true);
                }
                break;
            case R.id.fragment_mt_iv_foot:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.ZUB, true);
                }
                break;

        }
    }


    @Override
    public void getAirPosition(int position,int isrun) {
        myHandler.obtainMessage(2,position,isrun).sendToTarget();
    }
    private void setPosition(int position,boolean isSelect){
        mIvAirPosition.get(position).setSelected(isSelect);
    }

    @Override
    public void getAirStrong(int strong) {
        myHandler.obtainMessage(1,strong,0).sendToTarget();
    }

    @Override
    public void getTime(int min, int sec) {
          myHandler.obtainMessage(0,min,sec).sendToTarget();
    }

    @Override
    public void onChoose(int position) {
        mLayout.setTectColor(position);
        switch (position){
            case 0:
                mLlAirPosition.setVisibility(View.VISIBLE);
                mLlAirStrong.setVisibility(View.GONE);
                break;
            case 1:
                mLlAirPosition.setVisibility(View.GONE);
                mLlAirStrong.setVisibility(View.VISIBLE);
                break;
        }

    }

    private class MyHandler extends Handler {
        WeakReference<AirPressureMassageFragment> mWeakReference;

        MyHandler(AirPressureMassageFragment mFragment) {
            mWeakReference = new WeakReference<>(mFragment);
        }

        @Override
        public void handleMessage(Message msg) {
            final AirPressureMassageFragment mFragment = mWeakReference.get();
            if (mFragment != null) {
                switch (msg.what) {
                    case 0:
                        //时间
                        String min = String.valueOf(msg.arg1);
                        String sec = String.valueOf(msg.arg2);
                        if(min.length()==1){
                            min = "0"+min;
                        }
                        if(sec.length()==1){
                            sec = "0"+sec;
                        }
                        tvTime.setText(min + " : " + sec);
                        break;
                    case 1:
                        //强度
                        mLlAirStrong.setShow(msg.arg1);
                        break;
                    case 2:
                        //位置
                        boolean flag = (msg.arg2 == 1);
                        setPosition(msg.arg1, flag);
                        break;

                }
            }
        }
    }
}
